package com.mq.basic;

import com.mq.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 简单消息队列——生产者
 * @author Administrator
 *
 */
public class Sender {
	/**
	 * 队列名称
	 */
	private static final String QUEUE = "test_simple_queue";
	
	public static void main(String[] args) {
		Connection con = null;
		Channel channel = null;
		try {
			// 获取连接
			con = ConnectionUtils.getConnection();
			// 从连接中创建通道
			channel = con.createChannel();
			// 声明一个队列
			channel.queueDeclare(QUEUE, false, false, false, null);
			// 消息内容
			String msg = "java018 2th mqtest";
			// 发送消息
			channel.basicPublish("", QUEUE, null, msg.getBytes());
			System.out.println("send success");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接
			ConnectionUtils.close(channel, con);
		}
		
	}
 
}